# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
import logging
_logger = logging.getLogger(__name__)
WARNING_TYPES = [
    ('warning', '警告'), ('info', '信息'), ('error', '错误')]


class warning_box(models.TransientModel):
    _name = 'warning.box'
    _description = '信息提供'
    _req_name = 'title'

    type = fields.Selection(
        WARNING_TYPES,
        string='信息类型',
        readonly=True
    )
    title = fields.Char(
        string="提示框标题",
        size=100,
        readonly=True
    )
    message = fields.Text(
        string="消息内容",
        # readonly=True
    )
    def handle_remark_reason(self):
        m_name = self._context.get('m_name')
        m_id = self._context.get('m_id')
        remark_reason = self._context.get('remark_reason')
        state=self._context.get('state')
        obj = self.env[m_name].search([('id','=',m_id)])
        if obj:
            obj.write({'state':state,remark_reason:self.message})
        # return {"type": "ir.actions.act_window_close"}
        return {'type':'ir.actions.act_window_close'}

    def message_action(self):
        self.ensure_one()
        logging.error("-------------------")
        message_type = [t[1]for t in WARNING_TYPES if self.type == t[0]][0]
        logging.error(message_type)
        logging.error(self.id)
        logging.error(self._name)
        res = {
            'name': '%s: %s' % (_(message_type), _(self.title)),
            'view_type': 'form',
            'view_mode': 'form',
            # hnagri_demo.view_research_tree
            'view_id': self.env['ir.model.data'].xmlid_to_res_id( 'sys_tools.warning_box_form'),
            'res_model': 'warning.box',
            'domain': [],
            'context': self._context,
            'type': 'ir.actions.act_window',
            'target': 'new',
            'res_id': self.id
        }
        return res

    def warning(self, title, message):
        record = self.create({
            'title': title,
            'message': message,
            'type': 'warning'})
        return record.message_action()

    def info(self, title, message):
        record = self.create({
            'title': title,
            'message': message,
            'type': 'info'})
        return record.message_action()

    @api.model
    def error(self, title, message):
        record = self.create({
            'title': title,
            'message': message,
            'type': 'info'})
        return record.message_action()
